/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GoLib-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GoRedis.go			*/
/********************************************************/

package GoSql

import (
	"github.com/garyburd/redigo/redis"
)

type GoRedis struct {
	db	redis.Conn
}

func (r *GoRedis) GoRedisClose() {
	r.db.Close()
}

func (r *GoRedis) GoRedisClean() {
	r.__RedisCmd("FLUSHALL")
}

func (r *GoRedis) GoRedisSize() uint64 {
	t, _ := r.__RedisCmd("DBSIZE")
	return t.(uint64)
}

func (r *GoRedis) GoRedisKeys() []interface{} {
	return r.GoRedisValues("KEYS", "*")
}

func (r *GoRedis) GoRedisInt(v interface{}) int {
	i, _ := redis.Int(v, nil)
	return i
}

func (r *GoRedis) GoRedisBool(v interface{}) bool {
	b, _ := redis.Bool(v, nil)
	return b
}

func (r *GoRedis) GoRedisBytes(v interface{}) []byte {
	b, _ := redis.Bytes(v, nil)
	return b
}

func (r *GoRedis) GoRedisInt64(v interface{}) int64 {
	i, _ := redis.Int64(v, nil)
	return i
}

func (r *GoRedis) GoRedisUint64(v interface{}) uint64 {
	u, _ := redis.Uint64(v, nil)
	return u
}

func (r *GoRedis) GoRedisString(v interface{}) string {
	s, _ := redis.String(v, nil)
	return s
}

func (r *GoRedis) GoRedisFloat64(v interface{}) float64 {
	f, _ := redis.Float64(v, nil)
	return f
}

func (r *GoRedis) GoRedisStrings(v interface{}) []string {
	s, _ := redis.Strings(v, nil)
	return s
}

func (r *GoRedis) GoRedisOpen(net, add string) (err error) {
	r.db, err = redis.Dial(net, add)
	return err
}

func (r *GoRedis) GoRedisDelete(cmd string, args ...interface{}) error {
	_, err := r.__RedisCmd(cmd, args...)
	return err
}

func (r *GoRedis) GoRedisInsert(cmd string, args ...interface{}) error {
	_, err := r.__RedisCmd(cmd, args...)
	return err
}

func (r *GoRedis) GoRedisUpdate(cmd string, args ...interface{}) error {
	r.GoRedisDelete(cmd, args...)
	return r.GoRedisInsert(cmd, args...)
}

func (r *GoRedis) GoRedisSelect(cmd string, args ...interface{}) interface{} {
	v, _ := r.__RedisCmd(cmd, args...)
	return v
}

func (r *GoRedis) GoRedisScan(cmd string, args ...interface{}) []interface{} {
	return r.GoRedisValues(cmd, args...)
}

func (r *GoRedis) GoRedisValues(cmd string, args ...interface{}) []interface{} {
	v, _ := redis.Values(r.__RedisCmd(cmd, args...))
	return v
}

func (r *GoRedis) __RedisCmd(cmd string, args ...interface{}) (ret interface{}, err error) {
	return r.db.Do(cmd, args...)
}
